package com.cusc.module_login.factory;

import android.annotation.SuppressLint;
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.cusc.module_login.mvvm.model.LoginNetRegister;
import com.cusc.module_login.mvvm.vm.FunctionViewModel;
import com.cusc.module_login.mvvm.vm.LoginViewModel;

/**
 * 作者: wangfei
 * 创建时期: 2020/3/19.
 * 包名: com.cusc.module_login.factory
 * 类名: OSAndroidMvvm
 */
public class LoginViewModuleFactory extends ViewModelProvider.NewInstanceFactory{
    @SuppressLint("StaticFieldLeak")
    private static volatile LoginViewModuleFactory INSTANCE;
    private final Application mApplication;

    public static LoginViewModuleFactory getInstance(Application application) {
        if (INSTANCE == null) {
            synchronized (LoginViewModuleFactory.class) {
                if (INSTANCE == null) {
                    INSTANCE = new LoginViewModuleFactory(application);
                }
            }
        }
        return INSTANCE;
    }
    private LoginViewModuleFactory(Application application) {
        this.mApplication = application;
    }
    @VisibleForTesting
    public static void destroyInstance() {
        INSTANCE = null;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        if (modelClass.isAssignableFrom(LoginViewModel.class)) {
            return (T) new LoginViewModel(mApplication, new LoginNetRegister(mApplication));
        }
        if (modelClass.isAssignableFrom(FunctionViewModel.class)){
            return (T) new FunctionViewModel(mApplication,new LoginNetRegister(mApplication));
        }
        throw new IllegalArgumentException("Unknown ViewModel class: " + modelClass.getName());
    }
}
